package com.gzhryc.dictionary.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.dictionary.dao.db.CountryArea;
import com.gzhryc.dictionary.dao.enums.CountryAreaEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class WxCountryAreaHelper {

    static Logger log = Logger.getLogger(WxCountryAreaHelper.class);

    public static List<CountryArea> importJson(Long countryId,JsonEntity jsonEntity){
        Map<String,CountryArea> countryAreaMap = new HashMap<>();
        List<JoinItem> items = jsonEntity.result.get(0);
        for(JoinItem item : items){
            CountryArea countryArea = change(countryId,item,CountryAreaEnum.Type.Province.index());
            countryAreaMap.put(item.getId().substring(0,2),countryArea);
        }
        Map<String,CountryArea> cityAreaMap = new HashMap<>();
        for(int i =1;i<jsonEntity.result.size();i++) {
            items = jsonEntity.result.get(i);
            for (JoinItem item : items) {
                String provinceId = item.getId().substring(0, 2);
                CountryArea province = countryAreaMap.get(provinceId);
                if (province != null) {
                    String cityAreaId = item.getId().substring(4);
                    if (cityAreaId.equals("00")) {
                        if (province.getChildren() == null) {
                            province.setChildren(new ArrayList<>());
                        }
                        CountryArea city = change(countryId, item, CountryAreaEnum.Type.City.index());
                        province.getChildren().add(city);
                    } else {
                        String cityId = item.getId().substring(2, 4);
                        if (province.getChildren() == null) {
                            province.setChildren(new ArrayList<>());

                            CountryArea cityArea = change(countryId, item, CountryAreaEnum.Type.CityArea.index());
                            cityAreaMap.put(item.getId(), cityArea);
                        } else {
                            for (CountryArea city : province.getChildren()) {
                                String temp = city.getId().toString();
                                String cityTempId = temp.substring(2, 4);
                                if (cityTempId.equals(cityId)) {
                                    if (city.getChildren() == null) {
                                        city.setChildren(new ArrayList<>());
                                    }
                                    CountryArea cityArea = change(countryId, item, CountryAreaEnum.Type.CityArea.index());
                                    city.getChildren().add(cityArea);
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    log.error("市级未找到省级信息：{{0:json}}", item);
                }
            }
        }

        //处理未找到市级的地区
        if(cityAreaMap.size() > 0){
            for(String key : cityAreaMap.keySet()){
                String provinceId = key.substring(0, 2);
                String cityId = key.substring(2, 4);

                CountryArea province = countryAreaMap.get(provinceId);
                if (province != null) {
                    if (province.getChildren() != null) {
                        boolean flag = true;
                        for (CountryArea city : province.getChildren()) {
                            String temp = city.getId().toString();
                            String cityTempId = temp.substring(2, 4);
                            if (cityTempId.equals(cityId)) {
                                province.getChildren().add(cityAreaMap.get(key));
                                flag = false;
                                break;
                            }
                        }

                        if(flag){
                            log.error("区级未找到市级信息：{{0:json}}", cityAreaMap.get(key));
                        }
                    }else{
                        log.error("区级未添加：{{0:json}}", cityAreaMap.get(key));
                        log.error("省级的市级列表为空：{{0:json}}", province);
                    }
                }
            }
        }
        return new ArrayList<>(countryAreaMap.values());
    }

    public static CountryArea change(Long countryId,JoinItem item,Integer type){
        CountryArea countryArea = new CountryArea();
        countryArea.setId(Long.valueOf(item.getId()));
        countryArea.setCountryId(countryId);
        countryArea.setName(item.getName());
        countryArea.setFullName(item.getFullname());
        countryArea.setType(type);
        if(StringTools.isBlank(countryArea.getName())){
            countryArea.setName(item.getFullname());
        }
        if(item.getPinyin() != null && item.getPinyin().length > 0){
            countryArea.setPinyin(StringUtils.join(item.getPinyin(),","));
        }
        if(item.getLocation() != null) {
            countryArea.setLat(item.getLocation().getLat());
            countryArea.setLng(item.getLocation().getLng());
        }
        return countryArea;
    }

    public static class JsonEntity {

        private List<List<JoinItem>> result;

        public List<List<JoinItem>> getResult() {
            return result;
        }

        public void setResult(List<List<JoinItem>> result) {
            this.result = result;
        }
    }

    public static class JoinItem {

        private String id;
        private String name;
        private String fullname;
        private String[] pinyin;
        private Location location;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getFullname() {
            return fullname;
        }

        public void setFullname(String fullname) {
            this.fullname = fullname;
        }

        public String[] getPinyin() {
            return pinyin;
        }

        public void setPinyin(String[] pinyin) {
            this.pinyin = pinyin;
        }

        public Location getLocation() {
            return location;
        }

        public void setLocation(Location location) {
            this.location = location;
        }
    }

    public static class Location{

        private Double lat;
        private Double lng;

        public Double getLat() {
            return lat;
        }

        public void setLat(Double lat) {
            this.lat = lat;
        }

        public Double getLng() {
            return lng;
        }

        public void setLng(Double lng) {
            this.lng = lng;
        }
    }
}
